package com.example.zzl.view.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.widget.GridView;

import com.example.zzl.R;
import com.example.zzl.databinding.ActivityMainBinding;
import com.example.zzl.databinding.ClassifyfragmentLayoutBinding;
import com.example.zzl.view.BaseActivity;
import com.example.zzl.view.BaseFragment;
import com.example.zzl.viewmodel.SearchFragment.ResultData;

import com.example.zzl.R;
import com.example.zzl.databinding.ActivityMainBinding;
import com.example.zzl.view.BaseActivity;
import com.example.zzl.view.BaseFragment;
import com.example.zzl.viewmodel.activity.MainActivityPresenter;

import java.util.List;

public class MainActivity extends BaseActivity implements MainActivityPresenter.Callback{

    private ActivityMainBinding db;
    private MainActivityPresenter presenter;
    private List<BaseFragment> fragments;
    private int currentIndex = 0;
    private int targetIndex = 0;
    public static Handler handler,handler1;
    public static double sum;
    public static ResultData resultData = new ResultData();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db = DataBindingUtil.setContentView(this, R.layout.activity_main);
        presenter = new MainActivityPresenter(this, this, db);
        presenter.init();
        presenter.initView();
        fragments = presenter.getFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.linear, fragments.get(targetIndex)).commit();
    }

    @Override
    public void changeFragment(int position) {
        targetIndex = position;
        if (fragments.get(targetIndex).isAdded()) {
            getSupportFragmentManager().beginTransaction().hide(fragments.get(currentIndex)).show(fragments.get(targetIndex)).commit();
        } else {
            getSupportFragmentManager().beginTransaction().hide(fragments.get(currentIndex)).add(R.id.linear, fragments.get(targetIndex)).commit();
        }
        currentIndex = targetIndex;
    }
}
